package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CustomerReturnListGoodsServiceImpl
 * Description:
 * Date: 2022/1/18 0:01
 *
 * @version:
 * @since JDK 1.8
 */

@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {



    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public void saveCustomerReturnListGoods(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, HttpSession session) {


        User user = (User)session.getAttribute("currentUser");

        customerReturnList.setUserId(user.getUserId());

        customerReturnListGoodsDao.saveCustomerReturnList(customerReturnList);

        List<CustomerReturnListGoods> customerReturnListGoodsList = JSONObject.parseArray(customerReturnListGoodsStr, CustomerReturnListGoods.class);

        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {
            customerReturnListGoods.setCustomerReturnListId(customerReturnList.getCustomerReturnListId());
            customerReturnListGoodsDao.saveCustomerReturnListGoods(customerReturnListGoods);

        }




    }

    @Override
    public List<CustomerReturnList> getCustomerReturnListGoods(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<CustomerReturnList> customerReturnListList = customerReturnListGoodsDao.getCustomerReturnListGoods(returnNumber,customerId,state,sTime,eTime);
        return customerReturnListList;
    }

    @Override
    public List<CustomerReturnListGoods> getGoodsList(Integer customerReturnListId) {
        List<CustomerReturnListGoods>customerReturnListGoodsList = customerReturnListGoodsDao.getGoodsList(customerReturnListId);
        return customerReturnListGoodsList;
    }

    @Override
    public void deleteCustomerReturnListGoods(Integer customerReturnListId) {
        customerReturnListGoodsDao.deleteCustomerReturnListGoods(customerReturnListId);
        customerReturnListGoodsDao.deleteCustomerReturnList(customerReturnListId);
    }




    @Override
    public List<saleListGoodsCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        List<Integer> listGoodsIds = customerReturnListGoodsDao.getGoodsList1(goodsTypeId,codeOrName);

        List<CustomerReturnList> customerReturnListList= customerReturnListGoodsDao.getCustomerReturnListGoods(null, null,null,sTime,eTime);
        ArrayList<saleListGoodsCount> saleListGoodsCountArrayList = new ArrayList<>();
        for (CustomerReturnList customerReturnList : customerReturnListList) {
            String customerName = customerDao.selectById1(customerReturnList.getCustomerId());
            List<CustomerReturnListGoods> customerReturnListGoodsList = customerReturnListGoodsDao.getGoodsList(customerReturnList.getCustomerReturnListId());
            for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {

                String goodsTypeName = goodsTypeDao.selectById(customerReturnListGoods.getGoodsTypeId());

                saleListGoodsCount saleListGoodsCount = new saleListGoodsCount();

                saleListGoodsCount.setPurchaseListGoodsId(customerReturnListGoods.getCustomerReturnListGoodsId());
                saleListGoodsCount.setNumber(customerReturnList.getReturnNumber());
                saleListGoodsCount.setCustomerName(customerName);
                saleListGoodsCount.setDate(customerReturnList.getReturnDate());
                saleListGoodsCount.setCode(customerReturnListGoods.getGoodsCode());
                saleListGoodsCount.setName(customerReturnListGoods.getGoodsName());
                saleListGoodsCount.setModel(customerReturnListGoods.getGoodsModel());
                saleListGoodsCount.setGoodsType(goodsTypeName);
                saleListGoodsCount.setUnit(customerReturnListGoods.getGoodsUnit());
                saleListGoodsCount.setPrice(customerReturnListGoods.getPrice());
                saleListGoodsCount.setNum(customerReturnListGoods.getGoodsNum());
                saleListGoodsCount.setTotal(customerReturnListGoods.getTotal());
                saleListGoodsCountArrayList.add(saleListGoodsCount);
            }
        }


        ArrayList<saleListGoodsCount> purchaseListGoodsCountArrayList1 = new ArrayList<>();
        for (Integer listGoodsId : listGoodsIds) {
            for (saleListGoodsCount saleListGoodsCount : saleListGoodsCountArrayList) {
                if(listGoodsId.equals(saleListGoodsCount.getPurchaseListGoodsId())){
                    purchaseListGoodsCountArrayList1.add(saleListGoodsCount);
                }
            }
        }
        return purchaseListGoodsCountArrayList1;

    }

}
